package com.sh4.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sh4.dao.impl.RoleDaoImpl;
import com.sh4.dao.impl.RolePermissionDaoImpl;
import com.sh4.entity.Role;
import com.sh4.entity.RolePermission;
import com.sh4.service.RoleService;
import com.sh4.util.ContextHeader;

import net.sf.json.JSONObject;

@Service("roleServiceImpl")
@Transactional
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	RoleDaoImpl roleDao;
	@Autowired
	RolePermissionDaoImpl rolePermissionDao;
	
	@Override
	public int insertRole(Role role) {
		if (role.getId() == null) {//新建Role
			role.setCreateDate(new Date());
			role.setCreateBy(ContextHeader.getUserCode());
			roleDao.add(role);
			
		}else {
			//更新Role
			role.setUpdateDate(new Date());
			role.setUpdateBy(ContextHeader.getUserCode());
			roleDao.saveOrUpdate(role);
			
		}
		return 1;
	}

	@Override
	public int deleteRoleById(String roleId) {
		// TODO Auto-generated method stub
		Role role = new Role();
		role.setId(roleId);
		try {
			roleDao.delete(role);
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
		return 1;
	}

	@Override
	public List<Map<String, Object>>  searchRolesBy(JSONObject obj) {
		// TODO Auto-generated method stub
		List<String> values = new ArrayList<String>();
		String sql = "SELECT role.ID_ AS roleId,"+
				"role.ROLE_NAME_ AS roleName,"+
				"role.CREATE_BY_ AS createBy,"+
				"role.CREATE_DATE_ AS createDate,\n"+
				"role.UPDATE_BY_ AS updateBy,\n"+
				"role.UPDATE_DATE_ AS updateDate,\n"+
				"role.REMARKS_ AS remarks,\n"+
				"role_permission.ID_ AS id,\n"+
				"permission.ID_  AS permissionId,\n"+
				"permission.PERMISSION_NAME_ AS permissionName\n"+
				"FROM\n"+
				"role_permission\n"+
				"INNER JOIN role ON (role.ID_ = role_permission.ROLE_ID_\n";
			if (obj.getString("roleName").length() > 0) {
				
				String roleName =  "'%" +obj.getString("roleName")+"%'";
				sql = sql + " AND ROLE_NAME_ LIKE " +roleName;
			}
			if (obj.getString("startTime").length() > 0 && obj.getString("endTime").length() > 0) {
				String start = getSqlTime(obj.getString("startTime"));
				String end = getSqlTime(obj.getString("endTime"));
				sql += "AND (role.CREATE_DATE_ BETWEEN '"+start+"' AND '"+end+"')";
			}	
				sql += ") INNER JOIN permission ON (permission.ID_ = role_permission.PRESSION_ID_\n";
			
			if (obj.getString("perName").length() > 0) {
				
				String perName =  "'%"+obj.getString("perName")+"%'";
				sql +=	" AND PERMISSION_NAME_ LIKE  " +perName;
						
			}
			sql += ")";
			System.out.println(sql);
			
		List<Map<String, Object>> list = roleDao.getArrayBySql(sql);
		
		return list;
	}

	@Override
	public List<Map<String, Object>> selectedRolesAll(JSONObject params, int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String,Object>();
		String sql = "SELECT role.ID_ AS roleId,\n" + 
				"role.ROLE_NAME_ AS roleName,\n" + 
				"role.CREATE_BY_ AS createBy,\n" +
				"role.CREATE_DATE_ AS createDate,\n"+
				"role.UPDATE_BY_ AS updateBy,\n"+
				"role.UPDATE_DATE_ AS updateDate,\n" +
				"role.REMARKS_ AS remarks,\n" +
				"role_permission.ID_ AS id,\n" + 
				"permission.ID_  AS permissionId,\n" + 
				"permission.PERMISSION_NAME_ AS permissionName\n" + 
				"FROM\n" + 
				"role_permission\n" + 
				"INNER JOIN role ON (role.ID_ = role_permission.ROLE_ID_)\n" + 
				"INNER JOIN permission ON (permission.ID_ = role_permission.PRESSION_ID_)";
		List<Map<String, Object>> list = roleDao.getArrayPageBySql(pageNo, pageSize, sql );
		System.out.println(list);
		
		return list;
	}

	@Override
	public int addRoleByJsonObject(JSONObject params) {
		// TODO Auto-generated method stub
		
		Role role = new Role();
		RolePermission rolePermission = new RolePermission();
		if (params.getString("id") == null || params.getString("id").equals("")) {//增加
			
			role.setCreateDate(new Date());
			
		}else {//更新
			role.setId(params.getString("roleId"));
			
			rolePermission.setId(params.getString("id"));
			rolePermission.setRoleId(role.getId());
			rolePermission.setPermissionId(params.getString("permissionId"));
			role.setUpdateDate(new Date());
			role.setUpdateBy(ContextHeader.getUserCode());
		}
		role.setRemarks(params.getString("remarks"));
		role.setRoleName(params.getString("roleName"));
		roleDao.saveOrUpdate(role);
		//保存角色权限对象
		rolePermission.setRoleId(role.getId());
		rolePermission.setPermissionId(params.getString("permissionId"));
		
		rolePermissionDao.saveOrUpdate(rolePermission);
		
		
		return 1;
	}

	@Override
	public int deleteRoleAndRolePermission(JSONObject params) {
		// TODO Auto-generated method stub
		Role role = new Role();
		RolePermission rolePermission = new RolePermission();
		
		role.setId(params.getString("roleId"));
		rolePermission.setId(params.getString("id"));
		roleDao.delete(role);
		rolePermissionDao.delete(rolePermission);
	
		return 1;
	}

	@Override
	public List<Map<String, Object>> loginAll() {
		
		String sql= "SELECT r.ID_ as roleId , r.ROLE_NAME_ as roleName FROM role_permission rp INNER JOIN role r ON(rp.ROLE_ID_ = r.ID_)";
		
		return roleDao.getArrayBySql(sql);
	}
	
	private String getSqlTime(String date) {
		Date now = new Date(new Long(date) );
	     SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd  hh:mm:ss a zzz");
		
		return ft.format(now);
	}

}
